package com.quanshu.exchange.ui.gui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.hyphenate.EMCallBack;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.chat.ChatHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jye on 2018/2/3.
 */

public class LoginAnotherDeviceActivity extends BaseActivity {

    @BindView(R.id.ok_btn)
    TextView okBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_changed_and_logout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        okBtn.setOnClickListener(view -> doLoginOut());
    }

    private void doLoginOut() {
        ChatHelper.getInstance().logout(true, new EMCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(() -> {
                    MyApplication.getInstance().setCurrentUser(null);
                    currentUser = null;
                    ToastHelper.toast("退出登录成功");
                    Intent intent = new Intent(LoginAnotherDeviceActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                runOnUiThread(() -> {
                    Intent intent = new Intent(LoginAnotherDeviceActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                });
            }
        });
    }

    @Override
    public void finish() {
    }
}
